﻿<?php
include_once("./function.php");
//print_r($_POST);die;//Array ( [title] => 擦擦擦 [id] => 0 [author] => 才v [keyword] => 民谣 [description] => 才 [text] => 擦擦擦 [pid] => 31 [sub] => 修改 )

//print_r($_FILES);die;//Array ( [img] => Array ( [name] => Array ( [0] => ) [type] => Array ( [0] => ) [tmp_name] => Array ( [0] => ) [error] => Array ( [0] => 4 ) [size] => Array ( [0] => 0 ) ) )
extract($_POST);//$title $id $author $keyword $description $text
extract($_FILES);//$img
$pid=trim($pid);
$title=trim($title);
$author=trim($author);
$description=trim($description);
//$img=implode(',',$img);
$time=trim(time());
/*mysqli_query("UPDATE ts_news SET num = num+1");
$result = mysqli_query("SELECT * FROM ts_news");
while($row = mysql_fetch_array($result))
{
echo "<p style='font-size:100px; color:#f00;'>$row[num]</p>";
}*/
function get_dwj($file)
{
	if(empty(implode("",$file["name"])))
	{
		return "无文件上传";
	}

	$dir="./uploads/";
	if(empty($dir))
	{
		mkdir($dir,0777,true);
	}
		$res=array();$res1="";
		for($i=0;$i<count($file["name"]);$i++)
		{
			$num=$i+1;
		if(empty($file["name"][$i]))
		{
			$res1.="第{$num}个文件没有上传";
			continue;
		}

	$type=substr(strrchr($file["name"][$i],"."),1);
	$all=["jpg","jpeg","png","bmp","gif"];
	if(!in_array($type,$all))
	{
		$res1.=("第{$num}个文件类型不符合");
	}
	switch($file["error"][$i])
	{
		case 1:
			$err="超出服务器大小";
		break;
			case 2:
				$err="超出隐藏域设置的大小";
			break;
			case 3:
				$err="部分上传";
			break;
			case 4:
				$err="没有上传";
			break;
			case 6:
				$err="找不到临时文件夹";
			break;
			case 7:
				$err="写入临时文件夹出错";
			break;
	}
	if($file["error"][$i]!=0)
	{
		$res1.= $err;
		continue;
	}
	
	if(!is_uploaded_file($file["tmp_name"][$i]))
	{
		$res1.= "临时文件夹不存在";
		continue;
	}
	$path=$dir.date("His").rand(1000,9999).".".$type;
	if(move_uploaded_file($file["tmp_name"][$i],$path))
	{
		$res[]=$path;
	}
	else
	{
		$res1.= "第{$num}文件上传失败";	
	}
   }
	if($res1!="")
	{
		
		msg($res1,"product_add.php");exit;
	}
	else
	{
		return $res;
	}

}
$res=get_dwj($_FILES["img"]);
if(!empty($file["name"]))
{
 $res=get_dwj($_FILES["img"]);$img=implode(",",$res);
}
else
{
	$img="";
}
//print_r($img);echo "<br/>";

if(isset($_POST['pid']))
{
$sql="update ts_news set news_title='$title',news_content='$text',news_pubtime='$time',news_author='$author',news_descp='$description',news_keyword='$keyword',new_img='$img',news_click='99' where news_id=$pid";

$res=mysqli_query($link,$sql);
if($res)
{
	if(mysqli_affected_rows($link)==1)
	{
		msg("修改成功","./news.php");exit;
	}
	else
	{
		msg("修改失败","./news_edit.php");exit;
	}

}else
{
	echo "错误码:".mysqli_errno($link)."错误信息:".mysqli_error($link);exit;
}
}


?>